package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.ConferenceItemTypeDelegate;
import coms.dto.ConferenceItemType;
import coms.dto.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConferenceItemTypeAction extends ComsActionSupport implements IAction {

    private int conferenceItemTypeId;
    private ConferenceItemType conferenceItemType;
    private List<ConferenceItemType> conferenceItemTypeList;
    private ConferenceItemTypeDelegate conferenceItemTypeDelegate;
    private String action;
    private boolean admin;

    public boolean isAdmin() {
        User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public ConferenceItemTypeAction() {
        conferenceItemType = new ConferenceItemType();
        conferenceItemTypeDelegate = new ConferenceItemTypeDelegate();
        conferenceItemTypeList = new ArrayList<ConferenceItemType>();

    }

    private void initForm() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
    }

    private void populateDefault() {
        conferenceItemType = new ConferenceItemType();

    }

    private void populate() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItemType = conferenceItemTypeDelegate.getById(conferenceItemTypeId);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {

        initForm();
        populateDefault();
        action = "createConferenceItemType_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateConferenceItemType_" + conferenceItemType.getId();
        return "edit";
    }

    public String send() {
        populate();
        action = "sendConferenceItemType_" + conferenceItemType.getId();
        return "send";
    }

    @Override
    public String create() {

        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        conferenceItemTypeDelegate.create(conferenceItemType);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItemTypeDelegate.update(conferenceItemType);
        } catch (Exception ex) {
            return "error";
        }

        return "updated";
    }

    @Override
    public String delete() {
        populate();
        conferenceItemTypeDelegate.delete(conferenceItemType);
        return "deleted";
    }

    @Override
    public String findAll() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItemTypeList = conferenceItemTypeDelegate.findAll();
        } catch (Exception ex) {
            return "error";
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItemTypeList = conferenceItemTypeDelegate.findAll();
        } catch (Exception ex) {
            return "error";
        }

        return "search";
    }

    public ConferenceItemType getConferenceItemType() {
        return conferenceItemType;
    }

    public void setConferenceItemType(ConferenceItemType conferenceItemType) {
        this.conferenceItemType = conferenceItemType;
    }

    public List<ConferenceItemType> getConferenceItemTypeList() {
        return conferenceItemTypeList;
    }

    public void setConferenceItemTypeList(List<ConferenceItemType> conferenceItemTypeList) {
        this.conferenceItemTypeList = conferenceItemTypeList;
    }

    public void setConferenceItemTypeId(String conferenceItemTypeId) {
        try {
            this.conferenceItemTypeId = Integer.parseInt(conferenceItemTypeId);
            conferenceItemType.setId(this.conferenceItemTypeId);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getConferenceItemTypeId() {
        return "" + conferenceItemTypeId;
    }
}
